package string; 

class Css  {
	
	public var text_align:String;
	public var font_size:Float;
	var text_decoration:String;
	var margin:String;
	public var margin_top:Float;
	public var margin_bottom:Float;
	public var margin_left:Float;
	public var margin_right:Float;
	
	var padding:String;
	public var padding_top:Int;
	public var padding_bottom:Int;
	public var padding_left:Int;
	public var padding_right:Int;
	
	public var font_weight:String;
	public var font_style:String;
	public var font_family:String;
	public var color:Float;
	var stop_process:Float;  // Flag for disable checking
	public var background_colour:Int;
	public var background_colour_set:Bool;
	
	var display:String;
	
	public function new( txt:String ) {
		// To lower case
		
		padding_top =0;
		padding_bottom =0;
		padding_left =0;
		padding_right =0;
		// @meh... StringTools.toLowerCase(txt);
		txt = txt.toLowerCase();
		
		// monk.e.boy: remove the { and }
		txt =  StringTools.replace(txt, '{', '' );
		txt =  StringTools.replace(txt, '}', '' );
		
		// monk.e.boy: setup some default values.
		// does this confilct with 'clear()'?
		margin_top		= 0;
		margin_bottom	= 0;
		margin_left	= 0;
		margin_right	= 0;
		
		padding_top	= 0;
		padding_bottom	= 0;
		padding_left	= 0;
		padding_right	= 0;
	
		color = 0;
		background_colour_set = false;
		
		// Splitting by the ;
		var arr:Array<Dynamic> = txt.split(";"); //StringTools.split(txt,";"); // oops
		
		// Checking all the types of css params we accept and writing to Internal variables of the object class
		for( i in 0...arr.length)
		{
			getAttribute(arr[i]);
		}
	}
		
	function removeDoubleSpaces( txt:String ):String {
		var aux:String;
		var auxPrev:String;
		aux = txt;
		do {
			auxPrev = aux;
			//aux.replace('  ',' '); // TODO + Modify replace
			aux = StringTools.replace(aux, '  ',' ');
		} while (  auxPrev.length != aux.length  );
		return aux;
	}
	
	function convert2Float(cad:String):Float {
		cad =  StringTools.replace(cad, 'px', '' );		
		if (Math.isNaN( Std.parseFloat(cad)))
			return 0;
		return Std.parseFloat(cad);
	}
	
	function getAttribute( txt:String ):Void {
		var arr:Array<Dynamic> = txt.split(":");
		if( arr.length==2 )
		{
			stop_process = 1;
			set( arr[0], StringTools.trim(arr[1]) );
		}
	}
	/*
	public function get( cad:String ):Float {
		switch (cad) {
			case "text-align"			: return this.text_align;
			case "font-size"			: return convert2Float(this.font_size);
			case "text-decoration"		: return this.text_decoration;
			case "margin-top"			: return this.margin_top;
			case "margin-bottom"		: return this.margin_bottom;
			case "margin-left"			: return this.margin_left;
			case "margin-right"			: return this.margin_right;
			case "padding-top"			: return this.padding_top;
			case "padding-bottom"		: return this.padding_bottom;
			case "padding-left"			: return this.padding_left;
			case "padding-right"		: return this.padding_right;
			case "font-weight"			: return convert2Float(this.font_weight);
			case "font-style"			: return this.font_style;
			case "font-family"			: return this.font_family;
			case "color"				: return this.color;
			case "background-color"		: return this.bg_colour;
			case "display"				: return this.display;
			default						: return 0;
		}
	}
	*/


	// FUCKING!! Flash without By reference String parameters on functions
	public function set( cad:String, val:String ):Void {
		cad = StringTools.trim( cad );
	
		switch( cad )
		{
			case "text-align"		: text_align = val;
			case "font-size"		: set_font_size(val); // TODO - find missing function in original
			case "text-decoration"		: text_decoration = val;
			
			case "margin"			: setMargin(val); // TODO - find missing function in original	
			case "margin-top"		: margin_top = convert2Float(val); 
			case "margin-bottom"		: margin_bottom = convert2Float(val); 
			case "margin-left"		: margin_left = convert2Float(val); 
			case "margin-right"		: margin_right = convert2Float(val); 
			
			case 'padding'			: setPadding(val); // TODO - find missing function in original
			case "padding-top"		: padding_top = convert2Float(val); 
			case "padding-bottom"		: padding_bottom = convert2Float(val); 
			case "padding-left"		: padding_left = convert2Float(val); 
			case "padding-right"		: padding_right = convert2Float(val); 
			
			case "font-weight"		: font_weight = val; 
			case "font-style"		: font_style = val; 
			case "font-family"		: font_family = val; 
			case "color"			: set_color(val); // TODO - find missing function in original
			case "background-color":
				background_colour = Utils.get_colour(val);
				background_colour_set = true;
				
			case "display"				: display = val; 
		}
	}
}
	
